package com.aaa.behind.controller;


import com.aaa.behind.service.ITbLinkService;
import com.aaa.entity.TbLink;
import com.aaa.util.PageInfo;
import com.aaa.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author whj
 * @since 2023-02-01
 */
@RestController
@RequestMapping("/tb-link")
public class TbLinkController {

    @Autowired
    private ITbLinkService tbLinkService;

    @PostMapping("getAllLink")
    public Result getAllLink(@ModelAttribute PageInfo pageInfo, TbLink tbLink){
        return new Result(tbLinkService.getAllLink(pageInfo, tbLink));
    }

    @PostMapping("changeStatus/{linkId}/{isDisable}")
    public Result changeStatus(@PathVariable String linkId,@PathVariable String isDisable){
        TbLink tbLink= new TbLink();
        tbLink.setLinkId(linkId);
        tbLink.setIsDisable(isDisable);
        return new Result(tbLinkService.saveOrUpdate(tbLink));
    }

    @PostMapping("editLink")
    public Result editLink(TbLink tbLink){
        tbLink.setGmtModified(LocalDateTime());
        return new Result(tbLinkService.saveOrUpdate(tbLink));
    }

    @PostMapping("deleteLink/{linkId}")
    public Result deleteLink(@PathVariable String linkId){
        return new Result(tbLinkService.removeById(linkId));
    }

    @PostMapping("addLink")
    public Result addLink(TbLink tbLink){
        tbLink.setGmtCreate(LocalDateTime());
        tbLink.setGmtModified(LocalDateTime());
        tbLink.setIsDeleted(0);
        tbLink.setIsDisable("0");
        return new Result(tbLinkService.saveOrUpdate(tbLink));
    }

    public String LocalDateTime(){
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String format = dateTimeFormatter.format(now);
        return format;
    }
}

